﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Kenly.DBFramework.Gateway
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PagingResult
    {
        private int _pageIndex;
        private int _pageSize;
        private int _rowCount;
        private int _pageCount;

        /// <summary>
        /// Gets and sets PageIndex.
        /// </summary>
        public int PageIndex
        {
            get
            {
                return _pageIndex;
            }
            set
            {
                _pageIndex = value;
                this.Calculate();
            }
        }

        /// <summary>
        /// Gets and sets PageSize.
        /// </summary>
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                _pageSize = value;
                this.Calculate();
            }
        }

        /// <summary>
        /// Gets and sets RowCount.
        /// </summary>
        public int RowCount
        {
            get
            {
                return _rowCount;
            }
            set
            {
                _rowCount = value;
                this.Calculate();
            }
        }

        /// <summary>
        /// Gets PageCount.
        /// </summary>
        public int PageCount
        {
            get
            {
                return _pageCount;
            }
            private set
            {
                _pageCount = value;
            }
        }

        /// <summary>
        /// Gets CanMoveFirst.
        /// </summary>
        public bool CanMoveFirst
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets CanMoveLast.
        /// </summary>
        public bool CanMoveLast
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets CanMovePrevious.
        /// </summary>
        public bool CanMovePrevious
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets CanMoveNext.
        /// </summary>
        public bool CanMoveNext
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets NextPageIndex.
        /// </summary>
        public int NextPageIndex
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets PreviousPageIndex.
        /// </summary>
        public int PreviousPageIndex
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets and sets Entities.
        /// </summary>
        public IList Entities
        {
            get;
            set;
        }


        /// <summary>
        /// 
        /// </summary>
        public PagingResult()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entities"></param>
        public PagingResult(IList entities)
        {
            this.Entities = entities;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pagingArg"></param>
        /// <param name="entities"></param>
        public PagingResult(PagingArg pagingArg, IList entities)
            : this(pagingArg)
        {
            this.Entities = entities;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pagingArg"></param>
        public PagingResult(PagingArg pagingArg)
        {
            if (pagingArg == null ||
                pagingArg.PageCount == 0 || pagingArg.RowCount == 0)
            {
                return;
            }

            InheritPagingArg(pagingArg);
        }

        private void InheritPagingArg(PagingArg pagingArg)
        {
            //this.PageCount = pagingArg.PageCount;
            this.PageIndex = pagingArg.PageIndex;
            this.PageSize = pagingArg.PageSize;
            this.RowCount = pagingArg.RowCount;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Calculate()
        {
            this.CalculatePageCount();

            this.CanMoveFirst = this.PageIndex > 0;
            this.CanMoveLast = this.PageIndex < this.PageCount - 1;
            this.CanMovePrevious = this.PageIndex > 0;
            this.CanMoveNext = this.PageIndex < this.PageCount - 1;

            this.PreviousPageIndex = this.CanMovePrevious ?
                this.PageIndex - 1 : this.PageIndex;

            this.NextPageIndex = this.CanMoveNext ?
                this.PageIndex + 1 : this.PageIndex;
        }

        /// <summary>
        /// 
        /// </summary>
        private void CalculatePageCount()
        {
            if (this.PageSize <= 0)
            {
                return;
            }

            decimal pages = decimal.Divide(this.RowCount, this.PageSize);

            this.PageCount= (int)Math.Ceiling(pages);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "{PageIndex:" + this.PageIndex +
                ",PageSize:" + this.PageSize +
                ",PageCount:" + this.PageCount +
                ",RowCount:" + this.RowCount + "}";
        }
    }
}
